package com.sh.business.config;

import cn.hutool.core.util.ObjectUtil;
import com.sh.business.domain.dto.DelayMessageDto;
import com.sh.common.core.enums.MqQueueEnum;
import com.sh.common.json.utils.JsonUtils;
import com.sh.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author dongGe_zzz
 * @date 2024-03-22 19:47
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class RabbitmqSender {

    private final RabbitTemplate rabbitTemplate;
    private final ISysConfigService iSysConfigService;

    /**
     * 延迟查询时间间隔60秒
     */
    private static final Long DELAY = 60 * 1000L * 3;

    /**
     * 延迟24小时后
     */
    private static final Long DELAY_DAILY_DYNAMICS = 24 * 60 * 60 * 1000L;

    /**
     * 发送异步处理消息
     * @param object
     */
    public void sendCallBackMsg(Object object) {
        rabbitTemplate.convertAndSend(
            MqQueueEnum.CALLBACK_DIRECT.getExchange(),
            MqQueueEnum.CALLBACK_DIRECT.getRouteKey(),
            object);
        log.info("发送异步处理消息:{}", JsonUtils.toJsonString(object));
    }

    /**
     * 发送消息查询充值状态
     */
    public void sendTtlMsg(DelayMessageDto delayMessageDto, Long delay) {

        rabbitTemplate.convertAndSend(
            MqQueueEnum.TTL_DIRECT.getExchange(),
            MqQueueEnum.TTL_DIRECT.getRouteKey(),
            Objects.requireNonNull(JsonUtils.toJsonString(delayMessageDto)),
            correlationData -> {
                correlationData.getMessageProperties().setDelayLong(ObjectUtil.isNotNull(delay) ? delay : DELAY);
                return correlationData;
            });
        log.info("发送延迟查询交易状态消息:{}", JsonUtils.toJsonString(delayMessageDto));
    }

    /**
     * 发送消息24小时后执行szpn动态奖励
     */
    public void sendDailyDynamicsTtlMsg(String orderSn) {

        rabbitTemplate.convertAndSend(
            MqQueueEnum.TTL_DAILY_DYNAMICS_DIRECT.getExchange(),
            MqQueueEnum.TTL_DAILY_DYNAMICS_DIRECT.getRouteKey(),
            Objects.requireNonNull(orderSn),
            correlationData -> {
                correlationData.getMessageProperties().setDelayLong(DELAY_DAILY_DYNAMICS);
                return correlationData;
            });
        log.info("发送消息24小时后执行szpn动态奖励:{}", orderSn);
    }

    /**
     * 发送消息24小时后执行szpn动态奖励 - 手动补发消息
     */
    public void sendDailyDynamicsTtlMsgManual(String orderSn, Long delay) {

        rabbitTemplate.convertAndSend(
                MqQueueEnum.TTL_DAILY_DYNAMICS_DIRECT.getExchange(),
                MqQueueEnum.TTL_DAILY_DYNAMICS_DIRECT.getRouteKey(),
                Objects.requireNonNull(orderSn),
                correlationData -> {
                    correlationData.getMessageProperties().setDelayLong(delay * 1000L);
                    return correlationData;
                });
        log.info("发送消息24小时后执行szpn动态奖励:{}", orderSn);
    }

    /**
     * 发送消息24小时后执行szpn动态奖励 - 外部投资
     */
    public void sendDailyDynamicsOutTtlMsg(String orderSn, Long delay) {

        rabbitTemplate.convertAndSend(
            MqQueueEnum.TTL_DAILY_DYNAMICS_OUT_DIRECT.getExchange(),
            MqQueueEnum.TTL_DAILY_DYNAMICS_OUT_DIRECT.getRouteKey(),
            Objects.requireNonNull(orderSn),
            correlationData -> {
                correlationData.getMessageProperties().setDelayLong(ObjectUtil.isNotNull(delay) ? delay : DELAY_DAILY_DYNAMICS);
                return correlationData;
            });
        log.info("发送消息24小时后执行szpn动态奖励-外部:{}", orderSn);
    }


}
